//
// Created by hkkzzxz on 24-9-1.
//

#include "AdminController.h"
#include "AdminService.h"

void AdminController::addEmployee(std::string &data, http::request<http::dynamic_body> &httpRequest,
                                  http::response<http::dynamic_body> &httpResponse) {
    Json::Value root;
    Json::Value srcRoot;

    //尝试解析字符串
    if (parseRequestBody(httpRequest, srcRoot)) {
        auto id = srcRoot["id"].asString();
        auto name = srcRoot["name"].asString();
        auto phone_number = srcRoot["phone_number"].asString();

        std::unique_ptr<IAdminService> adminService = std::make_unique<AdminService>();
        bool result = adminService->addEmployee(id, name, phone_number);

        if (result) {
            httpResponse.result(http::status::ok);
            root["code"] = 0;
            root["message"] = "Successful add employee";
        } else {
            httpResponse.result(http::status::bad_request);
            root["code"] = 1002;
            root["message"] = "Failed add employee";
        }
    } else {
        httpResponse.result(http::status::bad_request);
        root["code"] = 1001;
        root["message"] = "Failed to parse JSON data!";
    }
    root["data"] = srcRoot;

    setHttpResponse(httpResponse, root);
}

void AdminController::setAllAttendanceRules(std::string &data, http::request<http::dynamic_body> &httpRequest,
                                            http::response<http::dynamic_body> &httpResponse) {
    Json::Value root;
    Json::Value srcRoot;

    //尝试解析字符串
    if (parseRequestBody(httpRequest, srcRoot)) {
        auto morningStartTime = srcRoot["morning_start_time"].asString();
        auto eveningEndTime = srcRoot["evening_end_time"].asString();

        std::unique_ptr<IAdminService> adminService = std::make_unique<AdminService>();
        bool result = adminService->setAllAttendanceRules(morningStartTime, eveningEndTime);

        if (result) {
            httpResponse.result(http::status::ok);
            root["code"] = 0;
            root["message"] = "Successfully set attendance rules";
        } else {
            httpResponse.result(http::status::bad_request);
            root["code"] = 1002;
            root["message"] = "Failed to set attendance rules";
        }
    } else {
        httpResponse.result(http::status::bad_request);
        root["code"] = 1001;
        root["message"] = "Failed to parse JSON data!";
    }
    root["data"] = srcRoot;

    setHttpResponse(httpResponse, root);
}

void AdminController::setAttendanceRules(std::string &data, http::request<http::dynamic_body> &httpRequest,
                                         http::response<http::dynamic_body> &httpResponse) {
    Json::Value root;
    Json::Value srcRoot;

    //尝试解析字符串
    if (parseRequestBody(httpRequest, srcRoot)) {
        auto id = srcRoot["id"].asString();
        auto morningStartTime = srcRoot["morning_start_time"].asString();
        auto eveningEndTime = srcRoot["evening_end_time"].asString();

        std::unique_ptr<IAdminService> adminService = std::make_unique<AdminService>();
        bool result = adminService->setAttendanceRules(id, morningStartTime, eveningEndTime);

        if (result) {
            httpResponse.result(http::status::ok);
            root["code"] = 0;
            root["message"] = "Successfully set attendance rules";
        } else {
            httpResponse.result(http::status::bad_request);
            root["code"] = 1002;
            root["message"] = "Failed to set attendance rules";
        }
    } else {
        httpResponse.result(http::status::bad_request);
        root["code"] = 1001;
        root["message"] = "Failed to parse JSON data!";
    }
    root["data"] = srcRoot;

    setHttpResponse(httpResponse, root);
}

void AdminController::deleteEmployee(std::string &data, http::request<http::dynamic_body> &httpRequest,
                                     http::response<http::dynamic_body> &httpResponse) {
    Json::Value root;
    Json::Value srcRoot;

    //尝试解析字符串
    if (parseRequestBody(httpRequest, srcRoot)) {
        auto id = srcRoot["id"].asString();
        auto name = srcRoot["name"].asString();

        std::unique_ptr<IAdminService> adminService = std::make_unique<AdminService>();
        bool result = adminService->deleteEmployee(id, name);

        if (result) {
            httpResponse.result(http::status::ok);
            root["code"] = 0;
            root["message"] = "Successful delete employee";
        } else {
            httpResponse.result(http::status::bad_request);
            root["code"] = 1002;
            root["message"] = "Failed delete employee";
        }
    } else {
        httpResponse.result(http::status::bad_request);
        root["code"] = 1001;
        root["message"] = "Failed to parse JSON data!";
    }
    root["data"] = srcRoot;

    setHttpResponse(httpResponse, root);
}

void AdminController::checkAttendanceRules(std::string &data, http::request<http::dynamic_body> &httpRequest,
                                           http::response<http::dynamic_body> &httpResponse) {
    Json::Value root;
    std::unique_ptr<IAdminService> adminService = std::make_unique<AdminService>();
    auto result = adminService->checkAttendanceRules();
    httpResponse.result(http::status::ok);
    root["code"] = 0;
    root["data"] = result;
    root["message"] = "Successful check attendance rules";
    setHttpResponse(httpResponse, root);
}

void AdminController::employeeInformation(std::string &data, http::request<http::dynamic_body> &httpRequest,
                                          http::response<http::dynamic_body> &httpResponse) {
    Json::Value root;
    std::unique_ptr<IAdminService> adminService = std::make_unique<AdminService>();
    auto result = adminService->employeeInformation();
    httpResponse.result(http::status::ok);
    root["code"] = 0;
    root["data"] = result;
    root["message"] = "Successful check attendance rules";
    setHttpResponse(httpResponse, root);
}

void AdminController::checkAttendance(std::string &data, http::request<http::dynamic_body> &httpRequest,
                                      http::response<http::dynamic_body> &httpResponse) {
    Json::Value root;
    std::unique_ptr<IAdminService> adminService = std::make_unique<AdminService>();
    auto result = adminService->checkAttendance();
    httpResponse.result(http::status::ok);
    root["code"] = 0;
    root["data"] = result;
    root["message"] = "Successful check all attendance";
    setHttpResponse(httpResponse, root);
}

void AdminController::exportReport(std::string &data, http::request<http::dynamic_body> &httpRequest,
                                   http::response<http::dynamic_body> &httpResponse) {
    std::unique_ptr<IAdminService> adminService = std::make_unique<AdminService>();
    auto result = adminService->exportReport();
    httpResponse.result(http::status::ok);
    httpResponse.set(http::field::content_type, "text/csv");
    beast::ostream(httpResponse.body()) << result;
}

void AdminController::synchronizeData(std::string &data, http::request<http::dynamic_body> &httpRequest,
                                      http::response<http::dynamic_body> &httpResponse) {
    Json::Value root;
    auto &body = httpRequest.body();
    auto bodyStr = boost::beast::buffers_to_string(body.data());
    std::unique_ptr<IAdminService> adminService = std::make_unique<AdminService>();
    if (adminService->synchronizeData(bodyStr)) {
        httpResponse.result(http::status::ok);
        root["code"] = 0;
        root["message"] = "Successful synchronize data";
    } else {
        httpResponse.result(http::status::bad_request);
        root["code"] = 1002;
        root["message"] = "Failed synchronize data";
    }
    root["data"] = "";
    setHttpResponse(httpResponse, root);
}
